package handler

import (
	"2301house/api/request"
	"2301house/api/resp"
	"2301house/internal/dao"
	"2301house/internal/model"
	"github.com/gin-gonic/gin"
)

func AddTime(c *gin.Context) {
	var req request.AddReservationTime
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}

	var resetva model.HouseReservation
	dao.GetOneByFields(&model.HouseReservation{
		UserId:  c.GetUint("userId"),
		HouseId: req.HouseId,
	}, &resetva)
	if resetva.Id != 0 {
		resp.Error(c, 10000, "当前预约已添加", nil)
		return
	}
	result := dao.Create(&model.HouseReservation{
		UserId:          c.GetUint("userId"),
		HouseId:         req.HouseId,
		ReservationTime: req.ReservationTime,
	})
	if !result {
		resp.Error(c, 10000, "预约看房失败", nil)
		return
	}
	resp.Success(c, "预约看房成功", nil)

}

func DelTime(c *gin.Context) {
	var req request.DelReservationTime
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}
	var resetva model.HouseReservation
	dao.GetOneByFields(&model.HouseReservation{
		Id: req.Id,
	}, &resetva)
	if resetva.Id == 0 {
		resp.Error(c, 10000, "当前预约信息不存在", nil)
		return
	}
	result := dao.Delete(&model.HouseReservation{
		Id: req.Id,
	})
	if !result {
		resp.Error(c, 10000, "取消看房失败", nil)
		return
	}
	resp.Success(c, "取消看房成功", nil)

}
